package com.wxy.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxy.common.utils.DozerUtil;
import com.wxy.web.dto.MenuDto;
import com.wxy.web.mapper.MenuMapper;
import com.wxy.web.model.Menu;
import com.wxy.web.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wxy
 * @since 2021-06-13
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    public DozerUtil dozerUtil;

    public List<MenuDto> listAll(){
        List<Menu> list = this.list();
        List<MenuDto> dtos = toTree(list,0);
        return dtos;
    }

    private List<MenuDto> toTree(List<Menu> list, Integer parentId) {
        List<MenuDto> tree = new ArrayList<>();
        List<Menu> children = list.stream().filter(s -> s.getParentId() == parentId).collect(Collectors.toList());
        for (Menu child : children) {
            MenuDto dto = dozerUtil.map(child,MenuDto.class);
            tree.add(dto);
            dto.setChildren(toTree(list, dto.getId()));
        }
        return tree;
    }

    public List<MenuDto> listByUserId(Integer userId){
        List<MenuDto> tree = new ArrayList<>();
        List<Menu> list = this.list();
        return tree;
    }
}
